package com.coupons.common;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.coupons.entity.Shop;
import com.coupons.entity.responseParams;

public class HttpOperator
{
	private static final int TIMEOUT = 60;
	private static final int BUFSIZE = 8192 * 5;
	public static String URL = "http://webservice.webxml.com.cn/";
	
	public Bitmap getHttpBitmap(String url) 
	{
		URL myFileUrl = null;
		Bitmap bitmap = null;
		try 
		{ 
			myFileUrl = new URL(url); 
		}
		catch (MalformedURLException e) 
		{
			e.printStackTrace(); 
		}
		try 
		{
			HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
			conn.setConnectTimeout(5000); 
			conn.setDoInput(true); 
			conn.connect(); 
			InputStream is = conn.getInputStream(); 
			bitmap = BitmapFactory.decodeStream(is); 
			is.close(); 
		}
		catch (IOException e) 
		{
			e.printStackTrace(); 
		}
		return bitmap;
	}
	public HttpParams createHttpParams() {

        final HttpParams params = new BasicHttpParams();

        HttpConnectionParams.setStaleCheckingEnabled(params, false);

        HttpConnectionParams.setConnectionTimeout(params, TIMEOUT * 1000);

        HttpConnectionParams.setSoTimeout(params, TIMEOUT * 1000);

        HttpConnectionParams.setSocketBufferSize(params, BUFSIZE);

        return params;

    }

	public String getUserRegisterString()
	{
		String resultstr = null;
		try
		{
			JSONObject json = new JSONObject();
			json.put("acid",1001);
			json.put("uid", "");
			json.put("pass","");
			resultstr = json.toString();
		}
		catch(JSONException e) 
		{
			return null;
		}

		finally
		{
			return resultstr;
		}
	}
	public String getLogonString(String uid, String password)
	{
		String resultstr = null;
		try
		{
			JSONObject json = new JSONObject();
			json.put("acid",1002);
			json.put("uid", uid);
			json.put("pass", password);
			resultstr = json.toString();
		}
		catch(JSONException e) 
		{
			return null;
		}

		finally
		{
			return resultstr;
		}
	}
	public String getLogoutString(String uid)
	{
		String resultstr = null;
		try
		{
			JSONObject json = new JSONObject();
			json.put("acid",1003);
			json.put("uid", uid);
			resultstr = json.toString();
		}
		catch(JSONException e) 
		{
			return null;
		}

		finally
		{
			return resultstr;
		}
	}
	
	public String getRequestListString(String sql)
	{
		String resultstr = null;
		try
		{
			JSONObject json = new JSONObject();
			json.put("acid",1004);
			json.put("sql", sql);
			resultstr = json.toString();
		}
		catch(JSONException e) 
		{
			return null;
		}

		finally
		{
			return resultstr;
		}
	}
	public String getRequestUpdateString(String version)
	{
		String resultstr = null;
		try
		{
			JSONObject json = new JSONObject();
			json.put("acid",1005);
			json.put("appVersion", version);
			resultstr = json.toString();
		}
		catch(JSONException e) 
		{
			return null;
		}

		finally
		{
			return resultstr;
		}
	}
	public String getOrderString(String sql)
	{
		String resultstr = null;
		try
		{
			JSONObject json = new JSONObject();
			json.put("acid",1006);
			json.put("sql", sql);
			resultstr = json.toString();
		}
		catch(JSONException e) 
		{
			return null;
		}

		finally
		{
			return resultstr;
		}
	}
	public void getRegisterResponse(String uid, String pass)
	{
		try
		{
			JSONObject jsonObject = new JSONObject();
			uid = jsonObject.getString("uid");
			pass = jsonObject.getString("pass");
		}
		catch(JSONException e) 
		{
			return ;
		}
		finally
		{
			return ;
		}
	}
	public ArrayList<Shop> getResponseList(String strResponse, responseParams params)
	{
		ArrayList<Shop> shops = null;
		try
		{
			JSONObject jsonObject = new JSONObject();
			params.sethasNext(jsonObject.getInt("hasNext"));
			params.setthisNum(jsonObject.getInt("thisNum"));
			JSONArray jsonArray = jsonObject.getJSONArray("dataList");
			shops = new ArrayList<Shop>(); 
			for(int i=0;i<jsonArray.length();i++)
			{
				JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i); 
				Shop shop = new Shop();
				shop.setShopId(jsonObject2.getString("shopId"));
				shop.setShopName(jsonObject2.getString("shopName"));
				shop.setShopIntro(jsonObject2.getString("shopIntro"));
				shop.setIconUrl(jsonObject2.getString("iconUrl"));
				shop.setShopRank(jsonObject2.getInt("rank"));
				shop.setShopAddress(jsonObject2.getString("shopAdress"));
				shop.setShopPhone(jsonObject2.getString("shopPhone"));
				shop.setConsumption(jsonObject2.getString("consumption"));
				shops.add(shop);
			}
		}
		catch(JSONException e) 
		{
			return null;
		}
		finally
		{
			return shops;
		}

	}
}
